package com.me.modules.problem.controller;

import java.util.*;

import com.me.modules.common.annotation.SysLog;
import com.me.modules.common.page.Page;
import com.me.modules.common.utils.*;
import com.me.modules.sys.entity.FieldDictionaryEntity;
import com.me.modules.sys.entity.OperationEntity;
import com.me.modules.sys.entity.ValueDictionaryEntity;
import com.me.modules.sys.service.FieldDictionaryService;
import com.me.modules.sys.service.OperationService;
import com.me.modules.sys.service.ValueDictionaryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.me.modules.problem.entity.ProblemInfoEntity;
import com.me.modules.problem.service.ProblemInfoService;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


/**
 * 问题工单信息表
 *
 * @author me
 * @email me@163.com
 * @date 2018-12-26 15:11:12
 */

@RequestMapping("/problem")
@Controller
public class ProblemInfoController {
	@Autowired
	private ProblemInfoService problemInfoService;
	@Autowired
	private FieldDictionaryService fieldDictionaryService;
	@Autowired
	private ValueDictionaryService valueDictionaryService;
	@Autowired
	private OperationService operationService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("probleminfo:all")
	@SysLog("查看问题列表")
	public String list( ProblemInfoEntity problemInfoEntity,Model model , HttpServletRequest request){
		int pageNum = Utils.parseInt(request.getParameter("pageNum"), 1);
		Page<ProblemInfoEntity> page = problemInfoService.findPage(problemInfoEntity, pageNum);
		List<ProblemInfoEntity>  problemList = page.getResult();
		List<ProblemInfoEntity>  newProblemList = new ArrayList<>();
		List<FieldDictionaryEntity> fieldList = fieldDictionaryService.queryByTableName("problem");
		if(fieldList.size()>0){
			if( problemList.size()>0){
				for (ProblemInfoEntity problemInfo : problemList) {
					String tableId = problemInfo.getId().toString();
					List<ValueDictionaryEntity> valueList = new ArrayList<>();
					for (FieldDictionaryEntity fieldDictionaryEntity : fieldList) {
						Long fieldID = fieldDictionaryEntity.getId();
						ValueDictionaryEntity value=valueDictionaryService.findByFieldIdAndUserId(fieldID,tableId);
						if(value!=null){
							valueList.add(value);
						}else{
							valueList.add(new ValueDictionaryEntity());
						}
					}
					problemInfo.setValueDictionarys(valueList);
					newProblemList.add(problemInfo);
				}
			}
			model.addAttribute("page",new Page(newProblemList,page.getTotal(),page.getPageSize() , page.getPageNum()));

		}else{
			model.addAttribute("page",page);
		}

		model.addAttribute("problem",problemInfoEntity);
		model.addAttribute("fieldList",fieldList);

		return "pro/problem";

	}


	/**
	 * 信息
	 */
	@RequestMapping("/info")
	@RequiresPermissions("probleminfo:all")
	@SysLog("查看问题工单信息")
	public String info(Model model , String id, HttpServletRequest request){
		List<FieldDictionaryEntity> fieldList = fieldDictionaryService.queryByTableName("problem");
		if(!StringUtils.isEmpty(id)){
			ProblemInfoEntity problemInfo = problemInfoService.queryObject(id);
			if(fieldList != null && fieldList.size() > 0){
				String tableId = problemInfo.getId().toString();
				List<ValueDictionaryEntity> valueList = new ArrayList<>();
				for (FieldDictionaryEntity fieldDictionaryEntity : fieldList) {
					Long fieldID = fieldDictionaryEntity.getId();
					ValueDictionaryEntity value = valueDictionaryService.findByFieldIdAndUserId(fieldID,tableId);
					if(value != null){
						valueList.add(value);
					}else{
						valueList.add(new ValueDictionaryEntity());
					}
				}
				problemInfo.setValueDictionarys(valueList);
			}

			model.addAttribute("problem",problemInfo);
		}
		model.addAttribute("fieldList",fieldList);
		//查询业务系统
		List<OperationEntity> operationList = operationService.queryListByBean(new OperationEntity());
		model.addAttribute("operationTree",JsonUtil.getJsonByObj(operationList));
		return "pro/problemEdit";
	}

	/**
	 * 编辑
	 * @param problemInfoEntity
	 * @return
	 */
	@RequestMapping(value = "edit",method = RequestMethod.POST)
	@RequiresPermissions("probleminfo:all")
	@ResponseBody
	public Result edit(ProblemInfoEntity problemInfoEntity){
		if(StringUtils.isEmpty(problemInfoEntity.getId())){
			try {
				problemInfoEntity.setProNo(Utils.getSN("pro_","yyyyMMddHHmmss",2));
				problemInfoEntity.setStatus("0");
				problemInfoEntity.setReqId(UserUtils.getCurrentUser().getId());
				problemInfoEntity.setCreateDate(new Date());
				problemInfoService.save(problemInfoEntity);
				if(problemInfoEntity.getValueDictionarys().size()>0) {
					for (ValueDictionaryEntity value : problemInfoEntity.getValueDictionarys()) {
						if(!StringUtils.isEmpty(value.getFieldValue())){
							value.setTableId(problemInfoEntity.getId().toString());
							valueDictionaryService.save(value);
						}
					}
				}
				return Result.ok("保存问题工单成功!");
			} catch (Exception e) {
				e.printStackTrace();
				return Result.error("保存问题工单失败!请联系管理员");
			}
		}else {
			try {
				problemInfoService.update(problemInfoEntity);
				if(problemInfoEntity.getValueDictionarys().size()>0) {
					for (ValueDictionaryEntity value : problemInfoEntity.getValueDictionarys()) {
						ValueDictionaryEntity valueDictionaryEntity =
								valueDictionaryService.findByFieldIdAndUserId(value.getFieldId(),problemInfoEntity.getId().toString());
						if(valueDictionaryEntity!=null){
							valueDictionaryService.delete(valueDictionaryEntity.getId());
						}
						if(!StringUtils.isEmpty(value.getFieldValue())){
							value.setTableId(problemInfoEntity.getId().toString());
							valueDictionaryService.save(value);
						}
					}
				}
				return Result.ok("修改问题工单成功!");
			} catch (Exception e) {
				e.printStackTrace();
				return Result.error("修改问题工单失败!请联系管理员");
			}
		}
	}

	/**
	 * 保存
	 */
//	@RequestMapping("/save")
//	@RequiresPermissions("probleminfo:all")
//	public Result save(@RequestBody ProblemInfoEntity problemInfo){
//		problemInfoService.save(problemInfo);
//
//		return Result.ok();
//	}

	/**
	 * 修改
	 */
//	@RequestMapping("/update")
//	@RequiresPermissions("probleminfo:update")
//	public Result update(@RequestBody ProblemInfoEntity problemInfo){
//		problemInfoService.update(problemInfo);
//
//		return Result.ok();
//	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "delete",method = RequestMethod.POST)
	@RequiresPermissions("probleminfo:all")
	@ResponseBody
	@SysLog("删除问题工单")
	public Result delete( String id){
		try {
			if(problemInfoService.delete(id) < 1){
				return Result.error("删除问题工单失败!请联系管理员");
			}
			return Result.ok("删除问题工单成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error("删除问题工单失败!请联系管理员");
		}
	}

}
